import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;

public class HybridFramework {
	String dURL, dSearchTerm, dEmail, dPswd, dSearchResults, dWrongEmailMsg, dWelcomeMsg;
	@Test
	public void myDriver() throws Exception{
		String[][] xTD, xTC, xTS;
		String vKW, vXPath, vTestData;
		long timeTaken;
		Date timeStart, timeEnd;
		

		//WebDriver myD = new FirefoxDriver();
		WebDriver myD = new InternetExplorerDriver();
		
		// Read the TD, TC and TS
		xTD = xl.readXL("C:\\Selenium\\A16_2012\\HFAutomation.xls", "Test Data");
		xTC = xl.readXL("C:\\Selenium\\A16_2012\\HFAutomation.xls", "Test Cases");
		xTS = xl.readXL("C:\\Selenium\\A16_2012\\HFAutomation.xls", "Test Steps");
		int vRows = xTC.length;
		int vCols = xTC[0].length;
		
		
		for (int h=1; h<xTD.length; h++) {// Go to each row in TD
			if (xTD[h][8].equalsIgnoreCase("Y")) {// Is the TD for execution
				
				// Read the values of the variables
				dURL = xTD[h][1];
				dSearchTerm = xTD[h][2];
				dEmail =  xTD[h][3];
				dPswd =  xTD[h][4];
				dSearchResults =  xTD[h][5];
				dWrongEmailMsg =  xTD[h][6];
				dWelcomeMsg = xTD[h][7];
				
				for (int i=1; i<xTC.length ; i++) {// Go to each Row in TC
					if (xTC[i][3].equalsIgnoreCase("Y")) { //identify which TC to execute
						xTC[i][4] = "Pass"; // Default TC is a pass
						timeStart = new Date(); // Returns seconds from some specific date in 1970's
						
						for (int k=1;k<xTS.length; k++){// Go to every TS 				 
							if (xTC[i][0].equalsIgnoreCase(xTS[k][1])) { // see if TCID's match
								
								vKW = xTS[k][4];
								vXPath = xTS[k][5];
								vTestData = xTS[k][6];
								String vError = "Pass";
								// Get the corresponding Test Data value based on the name of the TD Variable in TS
								vTestData = getTDValue(vTestData);
								try {
									// Call the corresponding Keyword Function
									if (vKW.equalsIgnoreCase("enterText")) {
										KW.enterText(myD, vXPath, vTestData);
									}
									if (vKW.equalsIgnoreCase("navigateBrowser")) {
										KW.navigateBrowser(myD,vTestData);
									}
									if (vKW.equalsIgnoreCase("clickElement")) {
										KW.clickElement(myD,vXPath);
									}
									if (vKW.equalsIgnoreCase("readText")) {
										KW.readText(myD,vXPath);
										System.out.println("Text here is " + vError);
									}
									if (vKW.equalsIgnoreCase("verifyText")) {
										vError = KW.verifyText(myD,vXPath, vTestData);
										System.out.println("Test Step is a " + vError);
									}
									if (vKW.equalsIgnoreCase("waitTime")) {
										KW.waitTime(5000);
									}
									if (!vError.equalsIgnoreCase("Pass")) {
										System.out.println("Pass: " + xTC[i][0] + "_" + vKW);
										xTS[k][7] = "Pass";
									} else {
										System.out.println("Fail: " + xTC[i][0] + "_" + vKW);
										xTS[k][7] = "Fail";
										xTS[k][8] = "Error happened as function returned Fail";
										xTC[i][4] = "Fail";
									}
									
								} catch(Exception selError) {
									System.out.println("Fail: " + xTC[i][0] + "_" + vKW);
									xTS[k][7] = "Fail";
									System.out.println("Error happened " + selError);
									xTS[k][8] = "Error happened " + selError;
									xTC[i][4] = "Fail";
									
								}
		
								
							}
						}
						timeEnd = new Date();
						timeTaken = (timeEnd.getTime() - timeStart.getTime())/1000;
						xTC[i][5] = timeTaken + " secs";
					}
				}

				// Write back the Test Steps with the error details into an excel
				xl.writeXL("C:\\Selenium\\A16_2012\\KDFAutomation-TCResult-"+xTD[h][0]+".xls", "TestCase Result", xTC);
				xl.writeXL("C:\\Selenium\\A16_2012\\KDFAutomation-TSResult-"+xTD[h][0]+".xls", "TestSteps Result", xTS);
			}
		}
		
		myD.quit();
	}

	private String getTDValue(String vTestData) {
		// TODO Auto-generated method stub
		if (vTestData.equals("dURL")){
			return dURL;
		}
		if (vTestData.equals("dSearchTerm")){
			return dSearchTerm;
		}
		if (vTestData.equals("dEmail")){
			return dEmail;
		}
		if (vTestData.equals("dPswd")){
			return dPswd;
		}
		if (vTestData.equals("dSearchResults")){
			return dSearchResults;
		}
		if (vTestData.equals("dWrongEmailMsg")){
			return dWrongEmailMsg;
		}
		if (vTestData.equals("dWelcomeMsg")){
			return dWelcomeMsg;
		}

		return vTestData;

	}
	
	public void executeKW(String vKW, String vXP, String vTD) {
		
	}
}
